<?php

namespace Modules\Admin\services;

use App\Exceptions\AdminLoginException;
use Modules\Admin\app\Models\AdminModel;
use App\Common\Enums\Code;

class LoginService extends BaseService
{
    /**
     * Undocumented function
     *
     * @return 
     */
    public function login(array $data)
    {
        $credential['username'] = $data['username'];
        $credential['password'] = $data['password'];

        $adminModel = AdminModel::where('username', $credential['username'])->first();
        
        if(!$adminModel->checkPassword($credential['password'])) {
            throw new AdminLoginException(Code::LOGIN_FAILED_ADMIN, '账号或密码不正确');
        }
        
        // 使用login方法直接登录
        $token = auth('admin_api')->login($adminModel);
        return $token;
    }
}